package cn.qy.kotlin.base

/**
 * 字符串
 */
const val TAVERN_NAME = "Taernyl's Folly"

fun main() {
    placeOrder("shandy,Dragon's Breath,5.91")
    placeOrder()
}

private fun placeOrder() {
    val indexOfApostrophe = TAVERN_NAME.indexOf('\'')
    // 区间范围，传入 IntRange 类型。若配合 until 则是含头不含尾
    val tavernMaster = TAVERN_NAME.substring(0 until indexOfApostrophe)
    println("Madrigal speaks with $tavernMaster about their order.")

    // 小客栈的酒水单数据使用字符串表示，用逗号分隔的饮料类型、饮料名和价格（金币）
    placeOrder("shandy,Dragon's Breath,5.91")
}

private fun placeOrder(menuData: String) {
    val indexOfApostrophe = TAVERN_NAME.indexOf('\'')
    val tavernMaster = TAVERN_NAME.substring(0 until indexOfApostrophe)
    println("Madrigal speaks with $tavernMaster about their order.")

    val data = menuData.split(',')
    val type = data[0]
    val name = data[1]
    val price = data[2]
    val message = "Madrigal buys a $name ($type) for $price."
    println(message)

    // 当然也可以 ”解构“ 酒水单数据
    // 解构常用来简化变量的赋值。只要是集合结果，就可以使用解构赋值。
    // 除了 List 集合，其他支持解构语法的数据类型有 Maps、Pairs 以及数据类
    val (type1, name1, price1) = menuData.split(',');
    val message1 = "Madrigal buys a $name1 ($type1) for $price1."
    println(message1)

    val phrase = "Ah, dElicious $name!"
    println("Madrigal exclaims: ${toDragonSpeak(phrase)}")
}

// 实现龙之语翻译
private fun toDragonSpeak(phrase: String) =
    phrase.replace(Regex("[aeiou]", RegexOption.IGNORE_CASE)) {
        when (it.value) {
            "a", "A" -> "4"
            "e", "E" -> "3"
            "i", "I" -> "1"
            "o", "O" -> "0"
            "u", "U" -> "|_|"
            else -> it.value
        }
    }